package com.xiyou.weblog.web.service.Impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiyou.weblog.common.domain.dos.ArticleCategoryRelDO;
import com.xiyou.weblog.common.domain.dos.ArticleDO;
import com.xiyou.weblog.common.domain.dos.CategoryDo;
import com.xiyou.weblog.common.domain.mapper.ArticleCategoryRelMapper;
import com.xiyou.weblog.common.domain.mapper.ArticleMapper;
import com.xiyou.weblog.common.domain.mapper.CategoryMapper;
import com.xiyou.weblog.common.enums.ResponseCodeEnum;
import com.xiyou.weblog.common.exception.BizException;
import com.xiyou.weblog.common.utils.PageResponse;
import com.xiyou.weblog.common.utils.Response;
import com.xiyou.weblog.web.model.vo.category.FindCategoryArticlePageListReqVO;
import com.xiyou.weblog.web.model.vo.category.FindCategoryArticlePageListRspVO;
import com.xiyou.weblog.web.model.vo.category.FindCategoryListRspVO;
import com.xiyou.weblog.web.service.CategoryService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author xiyuo
 * @description 首页分类模块
 */
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {

    @Resource
    private CategoryMapper categoryMapper;
    @Resource
    private ArticleMapper articleMapper;
    @Resource
    private ArticleCategoryRelMapper articleCategoryRelMapper;

    /**
     * 获取分类列表
     */
    @Override
    public Response findCategoryList() {
        // 所有分类
        List<CategoryDo> categoryDos = categoryMapper.selectList(Wrappers.emptyWrapper());

        // DO 转 VO
        List<FindCategoryListRspVO> vos = null;
        if (!CollectionUtils.isEmpty(categoryDos)) {
            vos = categoryDos.stream()
                    .map(categoryDO -> FindCategoryListRspVO.builder()
                            .id(categoryDO.getId())
                            .name(categoryDO.getName())
                            .build())
                    .toList();
        }
        return Response.success(vos);
    }

    /**
     * 根据分类ID查询文章
     */
    @Override
    public Response findCategoryArticlePageList(FindCategoryArticlePageListReqVO findCategoryArticlePageListReqVO) {
        Long current = findCategoryArticlePageListReqVO.getCurrent();
        Long size = findCategoryArticlePageListReqVO.getSize();
        Long categoryId = findCategoryArticlePageListReqVO.getId();

        CategoryDo categoryDo = categoryMapper.selectById(categoryId);

        // 判断分类是否存在
        if (Objects.isNull(categoryDo)) {
            log.warn("==> 该分类不存在，categoryId:{}", categoryId);
            throw new  BizException(ResponseCodeEnum.CATEGORY_NOT_EXISTED);
        }

        // 查询该分类关联的文章
        List<ArticleCategoryRelDO> articleCategoryRelDOS = articleCategoryRelMapper.selectListByCategoryId(categoryId);

        // 判断该分类是否有文章
        if (CollectionUtils.isEmpty(articleCategoryRelDOS)) {
            log.warn("==> 该分类未发布任何文章，categoryId:{}", categoryId);
            return PageResponse.fail("该分类未发布任何文章");
        }

        List<Long> articleIds = articleCategoryRelDOS.stream().map(ArticleCategoryRelDO::getArticleId).toList();

        //根据文章ID集合查询文章分页数据
        Page<ArticleDO> page = articleMapper.selectPageListByArticleIds(current,size,articleIds);
        List<ArticleDO> articleDOS = page.getRecords();

        // DO 转 VO
        List<FindCategoryArticlePageListRspVO> vos = null;
        if (CollectionUtils.isNotEmpty(articleDOS)) {
            vos = articleDOS.stream().map(articleDO -> FindCategoryArticlePageListRspVO.builder()
                    .id(articleDO.getId())
                    .title(articleDO.getTitle())
                    .cover(articleDO.getCover())
                    .createDate(articleDO.getCreateTime().toLocalDate())
                    .build())
                    .toList();
        }
        return PageResponse.success(page,vos);
    }
}
